/*
package com.agile.leetcode.map.traverse;


import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

*/
/**
 * @Author ChenZhangKun
 * @Date 2020/11/24 15:02
 *//*

@BenchmarkMode(Mode.AverageTime) // 测试完成时间
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 预热 2 轮，每次 1s
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 测试 5 轮，每次 1s
@Fork(1) // fork 1 个线程
@State(Scope.Thread) // 每个测试线程一个实例
public class TraverseMap {
    // 初始化map
    static Map<Integer, String> map = new HashMap<Integer, String>() {{
        for (int i = 0; i < 100; i++) {
            put(i, "val" + i);
        }
    }};

    public static void main(String[] args) throws RunnerException {
        // 启动基准测试
        Options opt = new OptionsBuilder()
                .include(TraverseMap.class.getSimpleName()) // 要导入的测试类
                .output("C:\\Users\\李赛飞\\Desktop\\jmh-map.log") // 输出测试结果的文件
                .build();
        new Runner(opt).run(); // 执行测试
    }

    */
/**
     * 遍历方式一 entry
     *//*

    @Benchmark
    public void entrySet() {
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> next = iterator.next();
            Integer i=next.getKey();
            String s=next.getValue();
        }
    }
    @Benchmark
    public void forEachEntrySet() {
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer i=entry.getKey();
            String s=entry.getValue();
        }
    }
    @Benchmark
    public void keySet(){
        Iterator<Integer> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            Integer next = iterator.next();
            String s=map.get(next);
        }
    }
    @Benchmark
    public void forEachKeySet(){
        Set<Integer> set = map.keySet();
        for (Integer integer : set) {
            String s=map.get(integer);
        }
    }
    @Benchmark
    public void lambda(){
        map.forEach((key,value)->{
            String s=map.get(key);
        });
    }
    @Benchmark
    public void streamSingleThread(){
        map.entrySet().stream().forEach(x->{
            Integer i=x.getKey();
            String s=x.getValue();
        });
    }
    public void streamMoreThread(){
        map.entrySet().parallelStream().forEach(x->{
            Integer key = x.getKey();
            String value = x.getValue();
        });
    }
}
*/
